package de.zainodis.triplog.data.persistence;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.j256.ormlite.dao.BaseDaoImpl;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.DatabaseTableConfig;

import java.sql.SQLException;
import java.util.List;

import de.zainodis.triplog.data.WeatherEntry;
import de.zainodis.triplog.data.share.TriplogContract;

/**
 * Created by fzarrai on 9/9/2014.
 */
public interface IWeatherDao extends Dao<WeatherEntry, Long> {

    //The table name for a WeatherEntry.
    public static final String TABLE_NAME = "weather";
    //Id of the weather table
    public static final String COLUMN_ID = "_id";
    //Forein key of the location table
    public static final String COLUMN_FK_LOCATION_ID = "location_id";
    //Column with the foreign key into the location table
    public static final String COLUMN_LOC_KEY = "location_id";
    //Date, stored as Text with format yyyy-MM-dd
    public static final String COLUMN_DATETEXT = "date";
    //Weather id as returned by API, to identify the icon to be used
    public static final String COLUMN_WEATHER_ICON_ID = "icon_id";
    //Short description and long description of the weather, as provided by API.  e.g "clear" vs "sky is clear"
    public static final String COLUMN_DESCRIPTION = "description";
    //Min and max temperatures for the day (stored as floats)
    public static final String COLUMN_MIN_TEMP = "min";
    public static final String COLUMN_MAX_TEMP = "max";
    //Humidity is stored as a float representing percentage
    public static final String COLUMN_HUMIDITY = "humidity";
    //Windspeed is stored as a float representing windspeed mph
    public static final String COLUMN_WIND_SPEED = "wind";
    /*
     * Degrees are meteorological degrees (e.g, 0 is north, 180 is south). Stored as floats.
     * Relevant for the wind.
     */
    public static final String COLUMN_DEGREES = "degrees";

    /**
     * Inserts the given list of weather content values into the database.
     *
     * @param context
     * @param entries the weather entries to insert into the database.
     * @return the number of inserted weather entries; -1 if an error occurred.
     */
    public int insertWeatherViaContentProvider(Context context, List<ContentValues> entries) throws
            SQLException;

    /**
     * Extracts the set date from the given weather entry and queries the database for matching entries.
     *
     * @param entry will be used as a date holder which we'll use for the query.
     * @return -1 if an error occurred or no match was found, the id of the match found. Generally the date is unique so it should find either one match or none.
     * @throws SQLException on error
     */
    public long queryForMatchingDate(WeatherEntry entry, Context context) throws SQLException;


    public class WeatherDao extends BaseDaoImpl<WeatherEntry, Long> implements IWeatherDao {
        private static final String TAG = "WeatherDao";

        public WeatherDao(ConnectionSource connectionSource) throws SQLException {
            super(connectionSource, WeatherEntry.class);
        }

        public WeatherDao(ConnectionSource connectionSource, DatabaseTableConfig<WeatherEntry> tableConfig)
                throws SQLException {
            super(connectionSource, tableConfig);
        }

        @Override
        public int insertWeatherViaContentProvider(Context context, List<ContentValues> entries) throws
                SQLException {
            //Run a bulk insert
            int count = context.getContentResolver().bulkInsert(TriplogContract.CONTENT_URI_WEATHER, entries.toArray(new ContentValues[entries.size()]));
            return count;
        }

        @Override
        public long queryForMatchingDate(WeatherEntry entry, Context context) throws SQLException {
            if (entry == null || entry.getDate() == null) {
                return -1;
            }

            Cursor cursor = context.getContentResolver().query(
                    TriplogContract.CONTENT_URI_WEATHER, new String[]{COLUMN_ID}, COLUMN_DATETEXT + " = ?",
                    new String[]{entry.getDate()},
                    null);
            if (cursor.moveToFirst()) {
                int id = cursor.getColumnIndex(COLUMN_ID);
                return cursor.getLong(id);
            }
            return -1;
        }

    }
}